Skip to content

Simplify logic in Diagonal-Tridiagonal multiplication #1338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented May 5, 2025

This improves performance in small ::Diagonal * ::Tridiagonal multiplications:

julia> D = Diagonal(1:3); T = Tridiagonal(1:2, 1:3, 1:2); C = similar(T);

julia> @btime mul!($C, $D, $T);
  43.435 ns (0 allocations: 0 bytes) # master
  14.725 ns (0 allocations: 0 bytes) # this PR

This also improves TTFX

julia> @time mul!(C, D, T);
  0.192970 seconds (451.55 k allocations: 22.211 MiB, 99.99% compilation time) # master
  0.129910 seconds (273.38 k allocations: 13.351 MiB, 99.98% compilation time) # this PR

@jishnub jishnub added performance Must go faster ttfx The change pertains to first-call latency labels May 5, 2025
Copy link

codecov bot commented May 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.74%. Comparing base (5165fd3) to head (993462b).
Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1338      +/-   ##
==========================================
- Coverage   93.74%   93.74%   -0.01%     
==========================================
  Files          34       34              
  Lines       15775    15766       -9     
==========================================
- Hits        14789    14780       -9     
  Misses        986      986              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jishnub jishnub requested a review from dkarrasch May 7, 2025 02:08
@dkarrasch
Copy link
Member

Do we have tests for those very small cases?

@jishnub
Copy link
Member Author

jishnub commented May 8, 2025

Yes, e.g.

@testset "mul for small matrices" begin
@testset for n in 0:6
D = Diagonal(rand(n))
v = rand(n)
@testset for uplo in (:L, :U)
B = Bidiagonal(rand(n), rand(max(n-1,0)), uplo)
M = Matrix(B)
@test B * v M * v
@test mul!(similar(v), B, v) M * v
@test mul!(ones(size(v)), B, v, 2, 3) M * v * 2 .+ 3
@test mul!(ones(size(v)), B, v, 0, 3) M * v * 0 .+ 3
@test B * B M * M
@test mul!(similar(B, size(B)), B, B) M * M
@test mul!(ones(size(B)), B, B, 2, 4) M * M * 2 .+ 4
@test mul!(ones(size(B)), B, B, 0, 4) M * M * 0 .+ 4
for m in 0:6
AL = rand(m,n)
AR = rand(n,m)
@test AL * B AL * M
@test B * AR M * AR
@test mul!(similar(AL), AL, B) AL * M
@test mul!(similar(AR), B, AR) M * AR
@test mul!(ones(size(AL)), AL, B, 2, 4) AL * M * 2 .+ 4
@test mul!(ones(size(AR)), B, AR, 2, 4) M * AR * 2 .+ 4
end
@test B * D M * D
@test D * B D * M
@test mul!(similar(B), B, D) M * D
@test mul!(similar(B), B, D) M * D
@test mul!(similar(B, size(B)), D, B) D * M
@test mul!(similar(B, size(B)), B, D) M * D
@test mul!(ones(size(B)), D, B, 2, 4) D * M * 2 .+ 4
@test mul!(ones(size(B)), B, D, 2, 4) M * D * 2 .+ 4
end
BL = Bidiagonal(rand(n), rand(max(0, n-1)), :L)
ML = Matrix(BL)
BU = Bidiagonal(rand(n), rand(max(0, n-1)), :U)
MU = Matrix(BU)
T = Tridiagonal(zeros(max(0, n-1)), zeros(n), zeros(max(0, n-1)))
@test mul!(T, BL, BU) ML * MU
@test mul!(T, BU, BL) MU * ML
T = Tridiagonal(ones(max(0, n-1)), ones(n), ones(max(0, n-1)))
@test mul!(copy(T), BL, BU, 2, 3) ML * MU * 2 + T * 3
@test mul!(copy(T), BU, BL, 2, 3) MU * ML * 2 + T * 3
end
n = 4
arr = SizedArrays.SizedArray{(2,2)}(reshape([1:4;],2,2))
for B in (
Bidiagonal(fill(arr,n), fill(arr,n-1), :L),
Bidiagonal(fill(arr,n), fill(arr,n-1), :U),
)
@test B * B Matrix(B) * Matrix(B)
BL = Bidiagonal(fill(arr,n), fill(arr,n-1), :L)
BU = Bidiagonal(fill(arr,n), fill(arr,n-1), :U)
@test BL * B Matrix(BL) * Matrix(B)
@test BU * B Matrix(BU) * Matrix(B)
@test B * BL Matrix(B) * Matrix(BL)
@test B * BU Matrix(B) * Matrix(BU)
D = Diagonal(fill(arr,n))
@test D * B Matrix(D) * Matrix(B)
@test B * D Matrix(B) * Matrix(D)
end
end

and similarly for Tridiagonal matrices.

@jishnub jishnub merged commit f0e36ea into master May 8, 2025
4 checks passed
@jishnub jishnub deleted the jishnub/dibimul branch May 8, 2025 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster ttfx The change pertains to first-call latency
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants